package com.liziguo.lzgfp.server.dao;

import com.liziguo.lzgfp.server.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author: Liziguo
 * @Date: 2021/12/26 15:27
 */
@Repository
public class UserDao {

    private static JdbcTemplate jdbcTemplate;

    public static int updateByKey(User user) {
        return jdbcTemplate.update("update user set remark=? where key=?", user.getRemark(), user.getKey());
    }

    public static List<User> selectAll() {
        return jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
    }

    public static User selectByKey(String key) {
        List<User> list = jdbcTemplate.query("select * from user where key=?", new BeanPropertyRowMapper<>(User.class), key);
        if (!list.isEmpty()) return list.get(0);
        return null;
    }

    public static int delete(int id) {
        return jdbcTemplate.update("delete from user where id=?", id);
    }

    public static int delete(String key) {
        return jdbcTemplate.update("delete from user where key=?", key);
    }

    public static int insert(String key) {
        return jdbcTemplate.update("insert into user(key,create_time) values(?,?)", key, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }

    @Autowired
    public void setJdbcTemplate(JdbcTemplate template) {
        jdbcTemplate = template;
    }
}
